package com.triones.framework.common.util.weixin;

import com.ruoyi.common.config.YjjExternalAuthConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.SSLContexts;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;

public class ReadSSL {

    private static ReadSSL readSSL = null;

    private ReadSSL(){

    }

    public static ReadSSL getInstance() {
        if (readSSL == null) {
            readSSL = new ReadSSL();
        }
        return readSSL;
    }

    /**
     * 读取 apiclient_cert.p12 证书
     * @return
     * @throws Exception
     */
    public SSLConnectionSocketFactory readCustomSSL() throws Exception{
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        //这是ca证书，需要你自己到商户端下载，并保存在本地，（如果功能部署到服务器，则需要配置服务器路径）
        FileInputStream instream = new FileInputStream(new File("D:\\triones\\商家岛\\支付证书\\1632502384_20240802_cert\\apiclient_cert.p12"));
        try {
//            keyStore.load(instream, yjjExternalAuthConfig.getMchid().toCharArray());
            keyStore.load(instream, "1632502384".toCharArray());
        } catch(Exception e){
            e.printStackTrace();
        } finally {
            instream.close();
        }
//        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, yjjExternalAuthConfig.getMchid().toCharArray()).build();
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "1632502384".toCharArray()).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] { "TLSv1" }, null,SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        return sslsf;
    }
}
